package com.whsb.api.controller;

import com.whsb.api.JdbcUtil;
import com.whsb.api.models.DataP1_1;
import com.whsb.api.models.DataP1_2;
import com.whsb.api.models.DataP1_3;
import com.whsb.api.models.ResponseBaseModel;
import oracle.jdbc.OracleTypes;
import org.springframework.web.bind.annotation.*;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/api/zwb") //在类上使用RequestMapping，里面设置的value就是方法的父路径
public class ZwbController {

    //这里体现了restful风格的请求，按照请求的类型，来进行增删查改。
    //设计restful api（其实也就是URL），不要有冗余，例如不要写成getUsers，URL中
    //最好不要有动词。
    @RequestMapping(method = RequestMethod.GET, value = "/p1/{gmsfhm}")
    public ResponseBaseModel p1(@PathVariable String gmsfhm) {

        ResponseBaseModel responseModel = null;
        try {
            Connection conn = JdbcUtil.getConnection();
            String sql = "call sb_zwb_api_01(?,?,?,?,?,?)";//该存储过程有6个参数
            CallableStatement cst = conn.prepareCall(sql);
            //设置in参数和out参数，jdbc以1为起始值
            cst.setString(1, gmsfhm);//in值
            cst.registerOutParameter(2, OracleTypes.VARCHAR);//out值
            cst.registerOutParameter(3, OracleTypes.VARCHAR);//out值
            cst.registerOutParameter(4, OracleTypes.CURSOR);//out值
            cst.registerOutParameter(5, OracleTypes.CURSOR);//out值
            cst.registerOutParameter(6, OracleTypes.CURSOR);//out值
            cst.execute();//抛行调用存储过程

            //依次接收5个返回值
            String fhz = cst.getString(2);
            String msg = cst.getString(3);

            switch(fhz) {
                case "1":
                    ResultSet result1 = (ResultSet) cst.getObject(4);//ResultSet表示返回的是集合数据
                    ResultSet result2 = (ResultSet) cst.getObject(5);

                    responseModel = new ResponseBaseModel<DataP1_1>();
                    responseModel.setFhz(fhz);
                    responseModel.setMsg(msg);

                    if (result1.next()) {
                        DataP1_1 data = new DataP1_1();
                        data.setXm(result1.getString("XM"));
                        data.setGrsxh(result1.getDouble("GRSXH"));
                        data.setGrbh(result1.getString("GRBH"));
                        data.setGmsfhm(result1.getString("GMSFHM"));
                        data.setSlcs(result1.getString("SLCS"));
                        data.setShiyedjrq(result1.getString("SHIYEDJRQ"));
                        data.setYfys(result1.getInt("YFYS"));
                        data.setSykfys(result1.getInt("SYKFYS"));
                        data.setDyzt(result1.getString("DYZT"));

                        List<DataP1_2> dyxx = new ArrayList<DataP1_2>();
                        while (result2.next()) {

                            DataP1_2 data2 = new DataP1_2();
                            data2.setGrsxh(result2.getDouble("GRSXH"));
                            data2.setYzny(result2.getString("YZNY"));
                            data2.setSyj(result2.getDouble("SYJ"));
                            data2.setYbf(result2.getDouble("YBF"));
                            data2.setWjbt(result2.getDouble("WJBT"));
                            data2.setSzf(result2.getDouble("SZF"));
                            data2.setYcxdy(result2.getDouble("YCXDY"));

                            dyxx.add(data2);
                        }
                        data.setDyxx(dyxx);
                        responseModel.setData(data);
                    }
                    break;

                case "2":
                    ResultSet result3 = (ResultSet) cst.getObject(6);

                    responseModel = new ResponseBaseModel<DataP1_3>();
                    responseModel.setFhz(fhz);
                    responseModel.setMsg(msg);

                    if (result3.next()) {

                        DataP1_3 data = new DataP1_3();
                        data.setXm(result3.getString("XM"));
                        data.setGrsxh(result3.getDouble("GRSXH"));
                        data.setGrbh(result3.getString("GRBH"));
                        data.setGmsfhm(result3.getString("GMSFHM"));
                        data.setGrcbzt(result3.getString("GRCBZT"));
                        data.setJfksny(result3.getString("JFKSNY"));
                        data.setJfzzny(result3.getString("JFZZNY"));
                        data.setJfys(result3.getInt("JFYS"));

                        responseModel.setData(data);
                    }
                    break;

                default:
                    responseModel = new ResponseBaseModel();
                    responseModel.setFhz(fhz);
                    responseModel.setMsg(msg);
                    break;
            }

            //关闭连接对象
            JdbcUtil.close(cst);
            JdbcUtil.close(conn);
        } catch (Exception ex) {
            responseModel = new ResponseBaseModel();
            responseModel.setFhz("-1");
            responseModel.setMsg("服务器发生错误，请联系管理员");
        }

        return responseModel;
    }
}
